package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mbc.common.entities.SystemProperty;

public class SystemPropertyDAOImpls extends DAO<SystemProperty> implements SystemPropertyDAO {

	public SystemPropertyDAOImpls(Connection conn){
		this.conn = conn;
		this.table_name = "SystemProperty";
	}
	
	@Override
	public SystemProperty getSystemProperty(String key) throws SQLException {
		SystemProperty l_SysProperty = null;
		query = getSelectQuery() + " where PKey = '" + key + "'";
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next())
			l_SysProperty = readRecord(rs);
		rs.close();
		pstmt.close();
		return l_SysProperty;
	}

	@Override
	protected SystemProperty readRecord(ResultSet rs) throws SQLException {
		SystemProperty l_SysProperty = new SystemProperty();
		l_SysProperty.setKey(rs.getString("PKey"));
		l_SysProperty.setValue(rs.getString("Value"));
		return l_SysProperty;
	}

	@Override
	public void create(SystemProperty entity) throws SQLException {
		query = "insert into " + table_name + "(PKey, Value) values( ?, ?)";
		pstmt = conn.prepareStatement(query);
		pstmt.setString(1, entity.getKey());
		pstmt.setString(2, entity.getValue());
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(SystemProperty entity) throws SQLException {
		query = "update " + table_name + " set [Value] = ? where PKey = ?";
		pstmt = conn.prepareStatement(query);
		pstmt.setString(1, entity.getValue());
		pstmt.setString(2, entity.getKey());
		pstmt.executeUpdate();
		pstmt.close();
	}

}
